import {
  listNetworks,
  updateContainer,
  startContainer,
  stopContainer,
  restartContainer,
  rebuildContainer,
} from '@/services/container/ContainerManagerController';
import { ProCard } from '@ant-design/pro-components';
import { Empty, message } from 'antd';
import React, { useState } from 'react';
import ContainerCard from './ContainerCard';

interface ContainerManagerProps {
  composes: API.DockerComposeVO[];
  loading: boolean;
  type: 'base' | 'app';
}

const ContainerManager: React.FC<ContainerManagerProps> = ({
  composes,
  loading,
  type,
}) => {
  const [expandedCards, setExpandedCards] = useState<Record<string, boolean>>(
    {},
  );
  const [editingCards, setEditingCards] = useState<Record<string, boolean>>({});

  // 获取网络列表
  const handleFetchNetworks = async () => {
    const result = await listNetworks();
      if (result.success && result.data) {
          return result.data;
      }
    return [];
  };

  // 保存配置
  const handleSaveConfig = async (
    id: string,
    data: Partial<API.DockerComposeVO>,
  ) => {
    await updateContainer(id, data);
  };
  const toggleCard = (id: string) => {
    setExpandedCards((prev) => ({
      ...prev,
      [id]: !prev[id],
    }));
  };

  const handleEditFile = (id: string) => {
    // 展开卡片并进入编辑模式
    setExpandedCards((prev) => ({ ...prev, [id]: true }));
    setEditingCards((prev) => ({ ...prev, [id]: true }));
  };

  const handleSaveEdit = async (
    id: string,
    data: Partial<API.DockerComposeVO>,
  ) => {
    try {
      await handleSaveConfig(id, data);
      message.success('配置保存成功，请重建');
      setEditingCards((prev) => ({ ...prev, [id]: false }));
    } catch (error) {
      message.error('配置保存失败');
    }
  };

  const handleCancelEdit = (id: string) => {
    setEditingCards((prev) => ({ ...prev, [id]: false }));
  };

  if (loading) {
    return <ProCard loading={loading} />;
  }

  if (composes.length === 0) {
    return (
      <ProCard>
        <Empty
          description={`未检测到${type === 'base' ? '基础应用' : '业务应用'}`}
        />
      </ProCard>
    );
  }



  // 启动容器
  const handleStart = async (id: string) => {
    try {
      await startContainer(id);
      message.success('容器启动成功');
    } catch (error) {
      message.error('容器启动失败');
    }
  };

  // 停止容器
  const handleStop = async (id: string) => {
    try {
      await stopContainer(id);
      message.success('容器停止成功');
    } catch (error) {
      message.error('容器停止失败');
    }
  };

  // 重启容器
  const handleRestart = async (id: string) => {
    try {
      await restartContainer(id);
      message.success('容器重启成功');
    } catch (error) {
      message.error('容器重启失败');
    }
  };

  // 重建容器
  const handleRebuild = async (id: string) => {
    try {
      await rebuildContainer(id);
      message.success('容器重建成功');
    } catch (error) {
      message.error('容器重建失败');
    }
  };

  return (
    <>
      {composes.map((item: API.DockerComposeVO) => (
        <ContainerCard
          key={item.id}
          item={item}
          expandedCards={expandedCards}
          editingCards={editingCards}
          onStart={handleStart}
          onStop={handleStop}
          onRestart={handleRestart}
          onRebuild={handleRebuild}
          onEditFile={handleEditFile}
          onSaveEdit={handleSaveEdit}
          onCancelEdit={handleCancelEdit}
          onToggle={toggleCard}
          onFetchNetworks={handleFetchNetworks}
        />
      ))}
    </>
  );
};

export default ContainerManager;
